﻿/// <reference path="vm.js" />
/// <reference path="../amplify.js" />
/// <reference path="../knockout-2.1.0.debug.js" />
/// <reference path="Repository.js" />
/// <reference path="../knockout.mapping-latest.debug.js" />
/// <reference path="../toastr.js" />

var AddNewClientViewModel = function(rawData) {
    var self = this;

    var mappingOptions = {
        RequestedDate: {
            create: function (option) {
                return dateFromJson(option.data).toDate();
            }
        },
        SubmittedDate: {
            create: function (option) {
                return dateFromJson(option.data).toDate();
            }
        }
    };

    ko.mapping.fromJS(rawData, mappingOptions, self);
    

    //properties
    
    self.isSelectedClient = ko.observable();
    
    self.messages = ko.observableArray();
    
    self.recentQuotes = ko.observableArray(repository.list.getRecentQuotes());

    self.gridView = ko.observable(new GridViewViewModel({ LineItems: self.LineItems() }));

    //computed properties
    self.isDisplaySplashScreen = ko.computed(function () {
        return !self.isSelectedClient();
    });

    self.isOCS = ko.computed({
        read: function() {
            return self.OCS();
        },
        write: function (newValue) {
            self.OCS(newValue==="true");
            self.Manufactured(!self.OCS());
        }
    });

    self.isSelectedLineItems = ko.computed(function () {
        var isSelect = false;
        
        var lineitems = self.gridView().LineItems();
        ko.utils.arrayForEach(lineitems, function(item) {
            if (item.Select()) {
                isSelect = true;
            }
        });
        
        return isSelect;
    });

    //Event handler
    self.activeClient = function (client) {
        if (client.data!=undefined) {
            self.Client.ClientName(client.data.ClientName);
            self.Client.JobName(client.data.JobName);
            self.Client.PONumber(client.data.PONumber);
            self.Client.Room(client.data.Room);
            
            self.isSelectedClient(true);

            log("Selected client " + client.data.ClientName);
        }
    };
    
    self.submitOrder = function () {
        var order = ko.toJSON(self);
        repository.order.submitOrder(order);
        log("Save order successfull");
        
    };

    self.refreshLineItems = function () {
        var data = {
            lineitems: repository.lineitem.getAll()
        };

        self.gridView(new GridViewViewModel(data));

        log("Refresh line item");
        return false;
    };

    self.addLineItemClick = function () {

        log("Add new empty line item");
        return false;
    };
  
    self.saveLineItems = function () {
        var lineitems = self.gridView().lineitems();
        var data = ko.toJSON(lineitems);
        repository.lineitem.save(data);

        log("Saved line items to server");
        return false;
    };

    var log = function (m) {
        var msg = new MessageModel();
        msg.time = (new Date()).toTime();
        msg.type = "info";
        msg.content = m;
        
        self.messages.push(msg);
    };

    return self;
}